﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using ASPItalia.ModelVirtualCasting.WPF.Views;
using ASPItalia.ModelVirtualCasting.WPF.ViewModels;
using ASPItalia.ModelVirtualCasting.WPF.Common.Messaging;
using System.Windows.Shell;
using System.Windows.Threading;
using ASPItalia.ModelVirtualCasting.WPF.DragDrop;

namespace ASPItalia.ModelVirtualCasting.WPF.Views
{
    /// <summary>
    /// Interaction logic for MainView.xaml
    /// </summary>
    public partial class MainView
    {

        private ImageSource recordImage;
        private DispatcherTimer recordTimer;

        public MainView()
        {
            InitializeComponent();

            PrepareRecordTimer();

            this.DataContext = ViewModelsContext.Current.MainViewModel;

            // Register for specific messages
            Messenger.Default.Register<ShowDialogMessage>(this, ShowDialog);
            Messenger.Default.Register<PropertyChangedMessage<Boolean>>(this, PropertyChanged);
            this.Loaded += new RoutedEventHandler(MainView_Loaded);
        }

        private void MainView_Loaded(object sender, RoutedEventArgs e)
        {
            ViewModelsContext.Current.MainViewModel.SelectStartupCandidate();
        }

        private void PropertyChanged(PropertyChangedMessage<Boolean> message)
        {
            switch (message.PropertyName)
            {
                case "IsRecording":
                    ShowRecordingOverlay(message.NewValue);
                    break;
                case "IsUploading":
                    ShowUploadProgress(message.NewValue);
                    break;
            }
        }

        private void ShowUploadProgress(bool show)
        {
            this.Dispatcher.BeginInvoke(() =>
            {
                if (show)
                    this.TaskbarItemInfo.ProgressState = TaskbarItemProgressState.Indeterminate;
                else
                    this.TaskbarItemInfo.ProgressState = TaskbarItemProgressState.None;
            });
        }

        #region Overlay icon

        private void PrepareRecordTimer()
        {
            this.recordTimer = new DispatcherTimer();
            this.recordTimer.Interval = TimeSpan.FromSeconds(1);
            this.recordTimer.Tick += new EventHandler(recordTimer_Tick);
            this.recordImage = BitmapFrame.Create(new Uri(@"pack://application:,,,/Resources/record.png"));
        }

        private void recordTimer_Tick(object sender, EventArgs e)
        {
            // Alternate icon overlay
            if (this.TaskbarItemInfo.Overlay == null)
                this.TaskbarItemInfo.Overlay = this.recordImage;
            else
                this.TaskbarItemInfo.Overlay = null;
        }

        private void ShowRecordingOverlay(bool show)
        {
            // Start the overlay record icon timer
            if (show)
                recordTimer.Start();
            else
            {
                this.recordTimer.Stop();
                // Remove icon overlay
                this.Dispatcher.BeginInvoke(new Action(() => this.TaskbarItemInfo.Overlay = null));
            }
        }

        #endregion

        private void ShowDialog(ShowDialogMessage message)
        {
            Window view;
            // Choose appropriate View depending on ViewModel type
            if (message.Content is NewCandidatesViewModel)
                view = new NewCandidatesView();
            else if (message.Content is ImageEditorViewModel)
                view = new ImageEditorView();
            else if (message.Content is MoviePlayerViewModel)
                view = new MoviePlayerView();
            else if (message.Content is RecordViewModel)
                view = new RecordView();
            else
                throw new NotSupportedException();

            view.DataContext = message.Content;
            view.Owner = this;

            bool? r = view.ShowDialog();

            // Notify the caller when the dialog is closed
            if (message.Callback != null)
                message.Callback(r);
        }


    }
}
